﻿using System;

namespace YArchitech.MEP
{
	public class HYFitting : HYComponent, IHasHostTube, IUpdateByHost
	{
		public double Angle { get; set; }

		public double Coefficient { get; set; }

		public HYTubeSize Size2
		{
			get
			{
				return this.m_Size2;
			}
			set
			{
				this.m_Size2 = value;
			}
		}

		public virtual void SetCoeffient(double coef, int partIndex)
		{
			if (partIndex == 0)
			{
				this.Coefficient = coef;
			}
		}

		public HYTube GetPreviousTube()
		{
			HYComponent previous = base.Previous;
			while (previous != null && !(previous is HYTube))
			{
				previous = previous.Previous;
			}
			if (previous == null)
			{
				return null;
			}
			return previous as HYTube;
		}

		public HYTube GetHostTube()
		{
			HYComponent next = base.Next;
			while (next != null && !(next is HYTube))
			{
				next = next.Next;
			}
			if (next == null)
			{
				return null;
			}
			return next as HYTube;
		}

		public virtual void UpdateSize()
		{
			HYTube hostTube = this.GetHostTube();
			if (hostTube != null)
			{
				this.m_Size = hostTube.Size;
			}
		}

		public override void Cal(HYSystemOption op)
		{
			base.Cal(op);
			this.CalCoefficient();
			this.AttachResistanceToTube();
		}

		public virtual void CalCoefficient()
		{
		}

		public virtual void AttachResistanceToTube()
		{
			HYTube hostTube = this.GetHostTube();
			if (hostTube != null)
			{
				hostTube.AddAttachment(this, this.Name, this.Coefficient, 0);
			}
		}

		protected HYTubeSize m_Size2;
	}
}
